<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<jsp:include page="/WEB-INF/page/layout/cssjs.jsp" />

<script type="text/javascript" src="${webPath}/lib/websocket/sockjs.js" ></script>
 
</head>
<body>
<div style="margin-left: 30px">
<h1>聊天室</h1>
<textarea id="msgArea" rows="10" cols="100">
</textarea>
<br/>
<input id="msgIpt" /><button id="sendBtn" >发送消息</button>

</div>



<script type="text/javascript">
// 首先判断是否 支持 WebSocket
if ('WebSocket' in window) {
	// ws 是web.xml 匹配规则
	ws=new WebSocket("ws://localhost:8080/ssm-maven-demo-3/ws/myHandler");
} else if ('MozWebSocket' in window) {
	ws = new MozWebSocket("ws://localhost:8080/ssm-maven-demo-3/ws/myHandler");
} else {
	//如果是低版本的浏览器，则用SockJS这个对象，对应了后台“sockjs/websocket”这个注册器，
	//它就是用来兼容低版本浏览器的
	// sjs 是web.xml 匹配规则
	ws = new SockJS("http://localhost:8080/ssm-maven-demo-3/sjs/sockjs/myHandler");   

}







$(function(){
	$("#sendBtn").click(function(){
		var msg=$("#msgIpt").val();
		ws.send(msg)
	});
	
})

// 开启连接
ws.onopen=function(e){
	console.log('链接成功')
	//ws.send("双旦快乐")
}
// 接受消息
ws.onmessage = function (event) {  
	 console.log('Received: ' + event.data);
	 var txt=$("#msgArea").val();
	 $("#msgArea").val(txt+"\r\n"+event.data)
	 
};  
// 连接关闭
ws.onclose = function (event) {  
	 console.log('Info: connection closed.');  
};  

</script>



</body>
</html>